package com.bw.zg3park2212.payler

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.entity.PaylerEntityItem
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.PaylerAdpter
import com.bw.zg3park2212.databinding.ActivityPaylerBinding
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/app/PaylerActivity")
class PaylerActivity : BaseActivity<ActivityPaylerBinding,PaylerViewModel>() {
    lateinit var paylerAdpter: PaylerAdpter
    var paylist= mutableListOf<PaylerEntityItem>()
    var month=SimpleDateFormat("MM").format(System.currentTimeMillis())
    var year=SimpleDateFormat("yyyy").format(System.currentTimeMillis())
    var type=""
    override fun intitView() {
        paylerAdpter= PaylerAdpter()
       binding.rvPaylist.adapter=paylerAdpter
        binding.rvPaylist.layoutManager=LinearLayoutManager(this)
        viewmodel.getpayler(2)
        binding.rgDate.setOnCheckedChangeListener { group, checkedId ->
            paylerAdpter.data.clear()
            when(checkedId){
                R.id.rb_month->{
                    paylerAdpter.addData(paylist.filter { it.paymentTime.split("-")[1]==month})
                }
                R.id.rb_year->{
                    paylerAdpter.addData(paylist.filter { it.paymentTime.split("-")[0]==year})
                }
            }
            paylerAdpter.notifyDataSetChanged()
        }
        var arr= mutableListOf<String>("全部", "物业费", "水电费","业务费")
        var adpter=ArrayAdapter(this,android.R.layout.simple_spinner_item,arr)
        adpter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.paylistProject.adapter=adpter
        binding.paylistProject.onItemSelectedListener= object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long,
            ) {
                paylerAdpter.data.clear()
                type=arr[position]
                if (position==0){
                    viewmodel.getpayler(2)
                }else{
                    paylerAdpter.addData(paylist.filter { it.chargeType==type})
                }
                paylerAdpter.notifyDataSetChanged()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }
        binding.keep.setOnClickListener {
            ARouter.getInstance().build("/app/PaylerAddActivity").navigation()
        }
        paylerAdpter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build( "/app/PaylerXiangActivity")
                .withSerializable("payler",paylerAdpter.data[position])
                .navigation()
        }
    }

    override fun intitData() {
        viewmodel.paylerSuccse.observe(this){
            paylerAdpter.data.clear()
            paylist=it
            paylerAdpter.addData(paylist.filter { it.paymentTime.split("-")[1]==month})
            paylerAdpter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        viewmodel.getpayler(2)
    }

}